#include "Turtle.h"

Turtle::Turtle()
{
	_vx=0.0000000000f;
	_vy=0.f;
}
Turtle::Turtle(float x, float y, float vx, float vy, int deltaTime,int resourceName,CResourceManager *rm):MObject(x,y,vx,vy,deltaTime,resourceName,rm)
{
	_objectType = CResourceManager::TYPE_TURTLE;
	_isMovable = true;
	_vx = -0.1f;
	_spriteMoveLeft = rm->getSprite(CResourceManager::TURBLEMOVELEFT);
	_spriteMoveRight = rm->getSprite(CResourceManager::TURBLEMOVERIGHT);
	_spritePushed = rm->getSprite(CResourceManager::TURBLEPUSHED);
	_spriteThrowing = rm->getSprite(CResourceManager::TURBLEDIE);
	_reAliveTime = deltaTime * delta;
	_immortalTime = 0;
	_status = STATUS_ALIVE;
	_recentChangeVel = false;
	_scored = 100;
	_info = CInfo(rm);
}
Turtle::Turtle(float x, float y, int Width, int Height, float vx, float vy, int deltaTime, LPD3DXSPRITE spriteHandler, LPDIRECT3DTEXTURE9 image):MObject(x,y,Width,Height,vx,vy,deltaTime,spriteHandler,image)
{
	
	_isMovable = true;

}
Turtle::~Turtle(void)
{
	//delete _spriteMoveLeft;
	//delete _spriteMoveRight;
	//delete _spritePushed;
	//delete _spriteThrowing;
}

void Turtle::Move()
{
	switch (_status)
	{
	case STATUS_ALIVE:
		{
			Vector v = _lastTranslateVector;
			float t1 = _lastTranslateVector.getY();
			float t2 = this->getVelocityVector().getY();
			float ty = t1 + t2;
	 
			if (!_isFalling || this->getLastTranslateVector() != Vector() &&(ty<=0))
				{
					_vy = 0; 
				}
				else
					if (_vy < 1.5f)
   						_vy += 0.15f;

			if (_y >600 - 32)
				setStatus(STATUS_DEAD);

			_lastX = _x;
			_lastY = _y;

			_x +=_vx*_deltaTime;
			_y +=_vy*_deltaTime;

			if (_vx >= 0)
				_sprite = _spriteMoveRight;
			else _sprite = _spriteMoveLeft;

			_lastTranslateVector = Vector();
			break;
		}

	case STATUS_PUSHED:
		{
			Vector v = _lastTranslateVector;
			float t1 = _lastTranslateVector.getY();
			float t2 = this->getVelocityVector().getY();
			float ty = t1 + t2;
	 
			if (!_isFalling || this->getLastTranslateVector() != Vector() &&(ty<=0))
				{
					_vy = 0; 
				}
				else
					if (_vy < 1.5f)
   						_vy += 0.15f;

			if (_y >600 - 32)
				setStatus(STATUS_DEAD);

			_lastX = _x;
			_lastY = _y;

			_x +=_vx*_deltaTime;
			_y +=_vy*_deltaTime;

			_lastTranslateVector = Vector();

			_reAliveTime -= _deltaTime;
			if (_immortalTime> 0) _immortalTime--;

			if (_reAliveTime <= 0)
			{
				setStatus(STATUS_ALIVE);
				_reAliveTime = _deltaTime * delta;
			}

			break;
		}
	

	case STATUS_THROWING:
		{
			Vector v = _lastTranslateVector;
			float t1 = _lastTranslateVector.getY();
			float t2 = this->getVelocityVector().getY();
			float ty = t1 + t2;
	 
					if (_vy < 1.5f)
   						_vy += 0.15f;

			if (_y >600 - 32)
				setStatus(STATUS_DEAD);

			_lastX = _x;
			_lastY = _y;

			_x +=_vx*_deltaTime;
			_y +=_vy*_deltaTime;

			_lastTranslateVector = Vector();

			break;
		}
		}
}

void Turtle::Render()
{
	Vector v = CScreen::getPosition();
	this->_sprite->Render(this->_x - v.getX(),this->_y -v.getY());
	if (_timeScore > 0)
	{
		_info.RenderNumber(_scored,_tx - v.getX(), _ty - v.getY());
		_timeScore--;
		_ty -= (0.05f*_deltaTime);
	}
}

void Turtle::actAfterCollided(int Direction, shared_ptr<MObject> Object)
{
	if (Object->getObjectType() == CResourceManager::TYPE_BULLET)
	{
		setStatus(STATUS_THROWING);
		Object->setStatus(CBullet::STATUS_DEAD);
		return;
	}
	switch(Direction)
	{
	case 1://UP
		{
			_vy = 0;
					if (Object->getObjectType() == CResourceManager::TYPE_BRICK && (Object->getStatus() == Brick::STATUS_SLIDE || Object->getStatus() == Brick::STATUS_BREAKED))
				setStatus(STATUS_THROWING);
			break;
		}
	//case -1:
	case 2: //DOWN
		{
			if ((Object->getObjectType()/10) == CResourceManager::TYPE_MARIO)
			{
				if (_status == STATUS_ALIVE)
				{
					setStatus(STATUS_PUSHED);
					//_immortalTime = 5;
				}
				else if (_status == STATUS_PUSHED)
					//if (_immortalTime <= 0)
					{
						if(_vx == 0)
						{
							/*if (Object->getVelocityVector().getX() > 0)
								_vx = 1.f;
							else _vx = -1.f;*/
							RECT rect = getRect();
							if (Object->getRect().right <= (rect.left + (float)(rect.right - rect.left)/2) + 5)
								_vx = 1.f;
							else
								if (Object->getRect().left >= (rect.left + (float)(rect.right - rect.left)/2) - 5)
								_vx = -1.f;
								else
									_vx = 0;
						}
						else 
							{
								_vx = 0;
						//		_immortalTime = 5;
							}
					}
			}
				//MessageBox(NULL,"I'm Dead!","Haha",MB_OK);
			break;
		}
	case 3: //RIGHT
		{
			if (Object->getObjectType() == CResourceManager::TYPE_TURTLE && Object->getStatus() == Turtle::STATUS_PUSHED && Object->getVelocityVector().getX() != 0)
			{
						setStatus(STATUS_THROWING);
						break;
			}

			if (Object->getObjectType()/10 == CResourceManager::TYPE_MARIO && _status == STATUS_PUSHED && _vx == 0)
			{
				_vx = -0.5f;
				break;
			}

			if (_recentChangeVel == false)
			{
				if (!((Object->getObjectType() == CResourceManager::TYPE_MUSHROOM||Object->getObjectType() == CResourceManager::TYPE_TURTLE || Object->getObjectType()/10 == CResourceManager::TYPE_MARIO) && _status == STATUS_PUSHED && _vx != 0))

				{
					if (Object->getPosition().getY() < _y + 20)
					{
						_vx = -_vx;
						_recentChangeVel = true;
					}
				}
			}
			else 
				_recentChangeVel = false;
		}
	case 4: //LEFT
		{
			if (Object->getObjectType() == CResourceManager::TYPE_TURTLE && Object->getStatus() == Turtle::STATUS_PUSHED&& Object->getVelocityVector().getX() != 0)
			{
						setStatus(STATUS_THROWING);
						break;
			}

			if (_status == STATUS_PUSHED && _vx == 0 && Object->getObjectType()/10 == CResourceManager::TYPE_MARIO)
				{
						_vx = 0.5f;
						break;
				}

			if (_recentChangeVel == false)
			{

				if (!((Object->getObjectType() == CResourceManager::TYPE_MUSHROOM||Object->getObjectType() == CResourceManager::TYPE_TURTLE || Object->getObjectType()/10 == CResourceManager::TYPE_MARIO) && _status == STATUS_PUSHED && _vx != 0))

				{
					if (Object->getPosition().getY() <= _y + 20)
					{					
						_vx = -_vx;
						_recentChangeVel = true;
					}
				}
			}
			else 
				_recentChangeVel = false;
				
			//_vy = -_vy;
			
			break;
		}
	case 0:
		break;
	}
}

void Turtle::setStatus(int Status)
{
	_status = Status;
	switch (_status)
	{
		case STATUS_ALIVE:
			{
				if (_vx < 0)
				{
					_sprite = _spriteMoveLeft;
					_vx = -0.1f;
				}
				else
				{
					_sprite = _spriteMoveRight;
					_vx = 0.1f;
				}
				break;
			}
		case STATUS_PUSHED:
			{
				_sprite = _spritePushed;
				_vx = 0;
				_timeScore = 100;
				_tx = _x;
				_ty = _y;
				CMarioSound::getInstance()->Play(CMarioSound::SOUND_ENEMYDIE,false);

			break;

			}

		case STATUS_THROWING:
			{
				_sprite = _spriteThrowing;
				_vx = 0;
				_vy = -1.3f;
				_timeScore = 100;
				_tx = _x;
				_ty = _y;
				CMarioSound::getInstance()->Play(CMarioSound::SOUND_ENEMYDIE,false);

			}
		case STATUS_DEAD:

			break;
	}
}

void Turtle::setVelocity(Vector v, shared_ptr<MObject> Object)
{

}
